//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 15
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace FirstProxySearchFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            FirstProxySearchFolder(binding);
        }

        /// <summary>
        /// Creates a search folder (Listing 15-23)
        /// </summary>
        /// <param name="binding">binding to use</param>
        ///
        public static void FirstProxySearchFolder(ExchangeServiceBinding binding)
        {
            // Create the request.
            //
            CreateFolderType request = new CreateFolderType();
            // Identify the parent folder.
            //
            request.ParentFolderId = new TargetFolderIdType();
            DistinguishedFolderIdType searchFolders = new
            DistinguishedFolderIdType();
            searchFolders.Id = DistinguishedFolderIdNameType.searchfolders;
            request.ParentFolderId.Item = searchFolders;
            // Identify a single folder to create.
            SearchFolderType mySearchFolder = new SearchFolderType();
            mySearchFolder.DisplayName = "MySearchFolder";
            // Identify the search parameter information.
            //
            mySearchFolder.SearchParameters = new SearchParametersType();
            mySearchFolder.SearchParameters.Traversal =
            SearchFolderTraversalType.Deep;
            mySearchFolder.SearchParameters.TraversalSpecified = true;
            // Identify the folders to search. Use just the inbox for right now.
            //
            DistinguishedFolderIdType[] folders = new DistinguishedFolderIdType[1];
            folders[0] = new DistinguishedFolderIdType();
            folders[0].Id = DistinguishedFolderIdNameType.inbox;
            mySearchFolder.SearchParameters.BaseFolderIds = folders;
            // Identify the restriction to filter the inbox items.
            //
            RestrictionType restriction = new RestrictionType();
            PathToUnindexedFieldType itemSubject = new PathToUnindexedFieldType();
            itemSubject.FieldURI = UnindexedFieldURIType.itemSubject;
            ContainsExpressionType contains = new ContainsExpressionType();
            contains.Item = itemSubject;
            ConstantValueType constant = new ConstantValueType();
            constant.Value = "Status";
            contains.Constant = constant;
            contains.ContainmentComparison = ContainmentComparisonType.Exact;
            contains.ContainmentComparisonSpecified = true;
            contains.ContainmentMode = ContainmentModeType.Substring;
            contains.ContainmentModeSpecified = true;
            restriction.Item = contains;
            // Set the restriction on our search parameters.
            //
            mySearchFolder.SearchParameters.Restriction = restriction;
            // Add the search folder to the request.
            //
            request.Folders = new BaseFolderType[1];
            request.Folders[0] = mySearchFolder;
            // Send the request and get the response.
            CreateFolderResponseType response = binding.CreateFolder(request);
            // Cast to the appropriate response type. We assume only a single
            // response message since a single search folder is created.
            FolderInfoResponseMessageType firmt = response.ResponseMessages.Items[0]
            as FolderInfoResponseMessageType;
            if (firmt.ResponseCode == ResponseCodeType.NoError)
            {
                Console.WriteLine("New folder id is :" +
                firmt.Folders[0].FolderId.Id);
            }
            else
            {
                Console.WriteLine("Folder not created. Error code: " +
                firmt.ResponseCode.ToString());
            }
        }
    }
}
